package com.stylefeng.guns.modular.system.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.core.util.DateUtil;
import com.stylefeng.guns.core.util.WebClientUtil;
import com.stylefeng.guns.modular.system.constants.ImportContants;
import com.stylefeng.guns.modular.system.dao.GdhsMapper;
import com.stylefeng.guns.modular.system.model.Gdhs;
import com.stylefeng.guns.modular.system.model.Gdtj;
import com.stylefeng.guns.modular.system.service.IGdhsService;
import com.stylefeng.guns.modular.system.service.IGdtjService;

/**
 * <p>
 * 股票户数 服务实现类
 * </p>
 *
 * @author stylefeng123
 * @since 2018-05-19
 */
@Service
@Transactional
public class GdhsServiceImpl extends ServiceImpl<GdhsMapper, Gdhs> implements IGdhsService {

	@Resource
    private GdhsMapper gdhsMapper;
	
	@Autowired
	private IGdtjService gdtjService;
	
	@Override
	public Gdhs getGdhsByDate(Gdhs gdhs) {
		return gdhsMapper.getGdhsByDate(gdhs);
	}

	@Override
	public List<Map<String, Object>> selectMapsByDm(Page<Gdhs> page, String beginTime,
			String endTime, String dm, Integer gtype, Integer ygsm, Integer grsm, Integer qfill, Integer jj, Integer gjd,
			Integer qynj,String name,String kpd,Integer zjc) {
		return gdhsMapper.selectMapsByDm(page,beginTime,endTime,dm,gtype,ygsm,grsm,qfill,jj,gjd,qynj,name,kpd,zjc);
	}

	@Override
	public boolean updateGudong(Integer gdhsId, String time) {
		Gdhs gdhs = gdhsMapper.selectById(gdhsId);
		if(gdhs == null){
			return false;
		}
		//解析流通股东
		Document document =  WebClientUtil.getHtmlPage(ImportContants.getYidianLTGDPathStr(gdhs.getgDm(), StringUtils.replace(time, "-", "_")), 5000);
		Elements elements =  document.select("tbody").select("tr");
		for(Element e : elements){
			Elements tds = e.select("td");
			if(tds.size() == 0) continue;
			
			String sharehdname = tds.get(0).text();
			Gdtj gdtj = new Gdtj();
			gdtj.setgRdate(DateUtil.parse(time, "yyyy-MM-dd"));
			gdtj.setgSharehdname(sharehdname);
			gdtj.setgDm(gdhs.getgDm());
			gdtj.setgSharestype("A股");
			gdtj  = gdtjService.selectGdTjByGdtj(gdtj);
			if(gdtj == null){
//				gdtj = new Gdtj();
//				gdtj.setgBdbl(entity.getBDBL());
//				gdtj.setgBz(entity.getBZ());
//				gdtj.setgCompanycode(entity.getCOMPANYCODE());
//				gdtj.setgDm(entity.getSCODE());
//				gdtj.setgRank(entity.getRANK());
//				gdtj.setgRdate(entity.getRDATE());
//				gdtj.setgSharehdname(entity.getSHAREHDNAME());
//				gdtj.setgSharehdnum(entity.getSHAREHDNUM());
//				gdtj.setgSharehdratio(entity.getSHAREHDRATIO());
//				gdtj.setgSharehdtype(entity.getSHAREHDTYPE());
//				gdtj.setgSharestype(entity.getSHARESTYPE());
//				gdtj.setgSname(entity.getSNAME());
//				gdtj.setgZb(entity.getZB());
//				gdtjService.insert(gdtj);
			}
		}
		return false;
	}
}
